package fr.neticar.webremoteclient.server;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.neticar.webremoteclient.server.application.IRemoteClient;


public class ImageServlet extends HttpServlet
{

    public ImageServlet()
    {

    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException
    {
        IRemoteClient client = getRemoteClient(req);
        String id = req.getParameter("id");
        ImageContentBean imageContent = client.getNextImage(id);
        if(imageContent == null)
        {
            return;
        } else
        {
            resp.setContentType(imageContent.getMimeType());
            resp.setContentLength(imageContent.getLength());
            OutputStream out = resp.getOutputStream();
            out.write(imageContent.getData().array());
            return;
        }
    }
    
	private IRemoteClient getRemoteClient(HttpServletRequest req) {
		HttpSession session = req.getSession();
		return (IRemoteClient) session.getAttribute(WRCClientGatewayImpl.REMOTE_CLIENT_SESSION_KEY);
	}
}
